import axios from "axios"
import { ElMessageBox } from 'element-plus'
import router from "@/router/index.js"
import nProgress from "nprogress"

let http = axios.create({
    //请求的基础地址
    baseURL: "/api",
    //请求超时设置
    timeout: 1000 * 10,
    //请求方式
    // method: 'post',
    // post请求携带参数
    // data:{....},
    //get请求传参
    // params: { key: value }
    // 请求头
    // headers: { key: value }
})




http.interceptors.request.use(config => {

    nProgress.set(0.5)
 
    let token = sessionStorage.getItem('tk');

    config.headers.authorization = token

    return config
})

http.interceptors.response.use(config => {


 
    nProgress.done()

   

    if (config.data.state) {
      

        return config
    } else {
     

        ElMessageBox.alert(config.data.msg, '提示', {
            type: 'error',
            showCancelButton: false,
            confirmButtonText: '确定'
        })

     

        if (config.data.code === '1004' || config.data.code === '10022') {
            router.replace('/login')
          
            sessionStorage.removeItem('tk')
         
            window.location.reload()
        }


        
        return Promise.reject(config.data.msg)
    }

})

export default http